﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import CommonJs from '../../utils/common'
import './activety.scss'

export default class Index extends Component {
  constructor(props) {
  	super(props);
    this.state = {
      navindex:1,//用于判断导航选中
      pageNumber:1,
      result:[],
      pageheight:0
    }
  }
  config = {
    navigationBarTitleText: '加载中'
  }

  componentWillMount () {
    var _this=this
    Taro.setNavigationBarTitle({title:"活动订单"})
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
      }
    })
    _this.getgoods(1)
  }

  getgoods(pageNumber){
    //获取商品列表
    // console.log(pageNumber+'pageNumber1')
    var _this=this
    var date1={
      status:1,
      pageSize:10,
      pageNumber:pageNumber
    }
    // console.log(JSON.stringify(date1)+'date1')
    CommonJs.ajaxRequest('/consume/queryConsumptionPageData',date1,'GET',function(res){
      // console.log(JSON.stringify(res)+'//33333333333333333'+_this.state.qid)
        if (res) {
          var hasNextPage = res.hasNextPage;
          var result = res.result;
          for(var i = 0; i < result.length; i++){
            _this.state.result.push(result[i]);
          }
          if (hasNextPage) {
            _this.setState({
              znwns: '暂无订单',
              pageNumber: res.pageNumber,
              hasNextPage: hasNextPage,
              scrollPd: true
            })
          } else {
            _this.setState({
              znwns: '暂无订单',
              pageNumber: res.pageNumber,
              hasNextPage: hasNextPage,
              scrollPd: false
            })
          }
        } else {
          _this.setState({
            znwns: '暂无订单'
          })
        }
    })
    
  }
  loadMore(){
    var _this = this;
    var pd = _this.state.scrollPd;
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1;
      _this.getgoods(pageNumber);
    }else{
      var hasNextPage =  _this.state.hasNextPage;
      if(hasNextPage){
        Taro.showToast({
            title:'正在加载数据...',
            icon: 'none',
            duration: 2000
          });
      }else{
        Taro.showToast({
            title:'没有更多数据啦！',
            icon: 'none',
            duration: 2000
          });
      }
    }
  }
  gettrefund(id){
    //申请退款
    // console.log(pageNumber+'pageNumber1')
    var _this=this
    var date={
      orderId:id
    }
    Taro.showModal({
      title:'温馨提示',
      content:'确定申请退款？',
      success(ret){
        if(ret.confirm == true){
          CommonJs.ajaxRequest('/consume/cancelConsume',date,'GET',function(res){
            // console.log(JSON.stringify(res)+'//888888')
             if(res){
              // console.log(JSON.stringify(res)+'//7777777')
              if (res.code!=300) {
                // console.log(JSON.stringify(res)+'//66666666')
                Taro.showModal({
                  title:'申请成功',
                  duration:1500,
                  showCancel:false,
                  success(ret){
                    _this.open('/pages/my/activety')
                  }
                })
              }
             }
          })
        }else{
          return false;
        }
      }
    })
  }
  open(url){
    CommonJs.open(url);
  }
  render () {
    const result = this.state.result
    return (
      <View className='page'>
        <View className='qun-bgbanner'>
          <Text className={this.state.navindex==1?'title activer':'title'} onClick={this.open.bind(this,'/pages/my/activety')}>待消费</Text>
          <Text className={this.state.navindex==2?'title activer':'title'} onClick={this.open.bind(this,'/pages/my/activety1')}>已消费</Text>
        </View>
        <ScrollView className='qun-goods' scrollY='true' style={'height:'+this.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
        <View className='qun-znwn' style={result.length!=0?'display:none':'display:block'}>{this.state.znwns}</View>
        {result.map((item, index) => (
        <View className='activety-content'>
          <View className='activety-title'>
            <View className='activety-number'>订单号：{item.orderSerialno}</View>
            <View className='activety-refund' onClick={this.gettrefund.bind(this,item.id)}>申请退款</View>
          </View>
          <View className='activety-goods'>
            <Image mode='scaleToFill' className='activety-pic' src={item.logo}></Image>
            <View className='activety-right'>
              <View className='activety-goodsname'>{item.project.name}</View>
              <View className='activety-time'>{CommonJs.FormatDate(item.createAt,3)}</View>
              <View className='activety-price'>消费金额：<Text className='activety-money'>￥{item.payAmount}</Text></View>
              <View className='activety-code'>验证码：{item.verifyCode}</View>
            </View>
          </View>
        </View>
        ))}
        </ScrollView>
      </View>
    )
  }
}

 